package com.edward.action;


import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.edward.JsonDateProcessor;
import com.edward.JsonTimeProcessor;
import com.edward.PagerModel;
import com.edward.model.ExistentInvoice;
import com.edward.model.InvoiceTrack;
import com.edward.service.InvoiceTrackManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InvoiceTrackAction extends ActionSupport {


	private static final long serialVersionUID = -4852643111361308365L;

	private InvoiceTrackManager invoiceTrackManager;
	
	private InvoiceTrack invoiceTrack;
	
	private ExistentInvoice existentInvoice;
	
	private int limit;
	
	private int start;
	
	private String query;
	
	private boolean flag = false;
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public InvoiceTrack getInvoiceTrack() {
		return invoiceTrack;
	}

	public void setInvoiceTrack(InvoiceTrack invoiceTrack) {
		this.invoiceTrack = invoiceTrack;
	}

	public void setInvoiceTrackManager(InvoiceTrackManager invoiceTrackManager) {
		this.invoiceTrackManager = invoiceTrackManager;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public ExistentInvoice getExistentInvoice() {
		return existentInvoice;
	}

	public void setExistentInvoice(ExistentInvoice existentInvoice) {
		this.existentInvoice = existentInvoice;
	}

	@Secured("ROLE_FINANCE")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.addInvoiceTrack(invoiceTrack);
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	

	@Secured("ROLE_FINANCE")
	public String update() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.updateInvoiceTrack(invoiceTrack);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String delete() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.deleteInvoiceTrack(invoiceTrack.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonTimeProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonTimeProcessor());
		PagerModel pager;
		if(flag){
			pager = invoiceTrackManager.pagerList(start,limit,query,invoiceTrack);
		}else{
			pager = invoiceTrackManager.pagerList(start,limit);
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		 
	           
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String addEI() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.addExistentInvoice(existentInvoice);
		 response.getWriter().print("{success:true,msg:'添加成功'}");
		return null;
	}
	

	@Secured("ROLE_FINANCE")
	public String updateEI() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.updateExistentInvoice(existentInvoice);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String deleteEI() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceTrackManager.deleteExistentInvoice(existentInvoice.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String getEIList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		pager = invoiceTrackManager.pagerEIList(start,limit);
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		 
	           
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String finish() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceTrack it = invoiceTrackManager.getInvoiceTrack(invoiceTrack.getId());
		if(it.getEndDate() != null){
			response.getWriter().print("{success:false, msg : '该记录已终结，无法再次终结!'}");
			return null;
		}
		existentInvoice.setDate(new Date());
		existentInvoice.setCompany(it.getCompany());
		existentInvoice.setName(it.getName());
		double money = (double)Math.round(it.getMoney()/1.17*100)/100;;
		double tax = (double)Math.round(it.getMoney()/1.17*0.17*100)/100;;
		existentInvoice.setMoney(money);
		existentInvoice.setTax(tax);
		invoiceTrackManager.addExistentInvoice(existentInvoice);
		it.setEndDate(new Date());
		invoiceTrackManager.updateInvoiceTrack(it);
		response.getWriter().print("{success : true , msg : '成功提交!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String changeAll() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceTrack it = invoiceTrackManager.getInvoiceTrack(invoiceTrack.getId());
		if(it.getEndDate() != null){
			response.getWriter().print("{success:false, msg : '该记录已终结，无法转换!'}");
			return null;
		}
		it.setEndDate(new Date());
		InvoiceTrack i = new InvoiceTrack();
		i.setCompany(it.getCompany());
		i.setDate(new Date());
		i.setName(it.getName());
		i.setMoney(invoiceTrack.getMoney());
		i.setType("已付全款未回");
		invoiceTrackManager.addInvoiceTrack(i);
		invoiceTrackManager.updateInvoiceTrack(it);
		response.getWriter().print("{success : true , msg : '成功提交!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	

	

	
	
	
	
}
